﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;
using ProductManagement.Ultilities;

namespace ProductManagement.Presentation_Layer
{
    public partial class ExportForm : MetroForm
    {
        private int _rid;
        private Validator validator = new Validator();
        private ProductManagementClassesDataContext db = new ProductManagementClassesDataContext();
        private MainForm _mainForm;

        public ExportForm(int reportID, MainForm mainForm)
        {
            _rid = reportID;
            _mainForm = mainForm;
            InitializeComponent();
        }

        private bool ValidateExportQuantity(int productID)
        {
            if (validator.ValidateQuantity(txtExportQuantity, errorProvider1))
            {
                int oldQuantity = db.sp_GetProductQuantity(productID).SingleOrDefault().quantity;
                int exportQuantity = Int32.Parse(txtExportQuantity.Text);
                if (exportQuantity > oldQuantity)
                {
                    errorProvider1.SetError(txtExportQuantity, "Số lượng xuất không được lớn hơn số lượng sản phẩm trong kho");
                    return false;
                }
                else
                    return true;
            }
            else
                return false;
        }

        private void btnAddExport_Click(object sender, EventArgs e)
        {
            try
            {
                int rowIndex = dgvExportProduct.SelectedCells[0].RowIndex;
                int productID = (int)dgvExportProduct[0, rowIndex].Value;
                string name = dgvExportProduct[1, rowIndex].Value.ToString();
                decimal price = (decimal)dgvExportProduct[2, rowIndex].Value;
                if (ValidateExportQuantity(productID))
                {
                    int exportQuantity = Int32.Parse(txtExportQuantity.Text);
                    int oldQuantity = (int)dgvExportProduct[3, rowIndex].Value;
                    db.sp_UpdateProductQuantity(oldQuantity - exportQuantity, productID);
                    db.sp_InsertReportDetail(_rid, name, price, exportQuantity);
                    db.SubmitChanges();
                }
            }
            catch (ArgumentOutOfRangeException)
            { 
            
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.ToString(), "Ngoại lệ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                dgvExportProduct.DataSource = db.sp_GetAllProducts();
                MainForm.DisplayExportImportInfo(_rid, _mainForm);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ExportForm_Load(object sender, EventArgs e)
        {
            dgvExportProduct.DataSource = db.sp_GetAllProducts();
        }
    }
}
